﻿var filesHtml = {};

$(function () {
    var url = new Url();
    var $orderId = url.GetQueryString("orderId");
    var serializer = new JSONSerializer();
    //获取并绑定数据
    $.getJSON("../Handler/LoanProductOrderHandler.ashx",
        { Type: 4, OrderId: $orderId, _: Math.random() },
        function (result) {
            if (result.Status) {
                updateProgress(result.Data);
                //绑定审核信息
                if (result.Data.Verify != null) {
                    if (result.Data.Verify.result.length != 0) {
                        $(".verifyOpinion").css("display", "inline");
                        $.each(result.Data.Verify.result, function (index, item) {
                            $("#opinionTemplate").tmpl(item).appendTo("#verifyOpinion");
                            //if (item.Name == "综评") {
                            //    $("#opinionTemplate").tmpl(item).appendTo("#verifyOpinion");
                            //}

                        });
                    }
                    ////单独绑定结算退回意见
                    //if (result.Data.Verify.BackOpinion != "") {
                    //    $("#account").css("display", "inline")
                    //        .children("span")
                    //        .text(result.Data.Verify.BackOpinion);
                    //    // .attr("disabled", "disabled")
                    //}
                }

                loadVerifyFiles(result.Data.Order.ProductTypeId);
                $.each(result.Data.LoanItems, function (index, item) {
                    if (!filesHtml["content" + item.AuthId]) {
                        filesHtml["content" + item.AuthId] = "";
                    }
                    $("#FileTemplate").tmpl(item.Files).appendTo($("#fileContent"));
                    filesHtml["content" + item.AuthId] += $("#fileContent").html();
                    $("#fileContent").empty();
                });
                bindImgData();
                //绑定用户信息
                serializer.DeSerializeToSpan($("#personalinfo"), result.Data.User);
                //绑定业务信息
                serializer.DeSerializeToSpan($("#businessInfo"), result.Data.Order);
                //绑定批核信息
                if (result.Data.Order) {
                    serializer.DeSerializeToSpan($("#approvedInfo"), result.Data.Order);
                    //根据月还款额判断是否存在批核信息
                    var repayPerMonth = $("span[data-name=RepayPerMonth]").text();
                    if (repayPerMonth != "") {
                        $(".loanGetInfo").css("display", "inline");
                        //单独绑定是否更换产品名称
                        $("div.isAccordanceProduct>span").text(result.Data.Order.IsAccordanceProduct ? "是" : "否");
                        if (result.Data.Order.IsAccordanceProduct) {
                            $("div.accordanceProduct").css("display", "block")
                                .children("span").text(result.Data.Order.ChangeProductName.split(',')[0]);
                        }
                        //单独绑定是否联名借款
                        $("div.isCoborrowLoan>span").text(result.Data.Order.IsCoborrowLoan ? "是" : "否");
                        if (result.Data.Order.IsCoborrowLoan) {
                            $("div.coborrowLoan").css("display", "block");
                            $("div.coborrowLoan>span").text(result.Data.Order.CoborrowName.split(',')[0]);
                        }
                    }
                    //判断是否有合同文件
                    if (result.Data.Order.AgreementId) {
                        $(".contractDoc").css("display", "block");
                    }
                }
                $("#position").text(result.Data.Work.Position);
                //绑定合同信息
                if (result.Data.ContractInfo) {
                    var contractInfo = result.Data.ContractInfo;
                    if (contractInfo.StartDate != null) {
                        $(".contractInfo").css("display", "inline");
                        serializer.DeSerializeToSpan($("#contractArea"), result.Data.ContractInfo);
                        $("span[data-name='StartDate']").text(ChangeDateFormat(contractInfo.StartDate));
                        $("span[data-name='EndDate']").text(ChangeDateFormat(contractInfo.EndDate));
                        $("span[data-name='SignDate']").text(ChangeDateFormat(contractInfo.SignDate));
                    }
                }

                if (result.Data.Order.IsAgencyContract == 1) {
                    $("div.orgBankInfo").css("display", "block");
                    //查询机构信息
                    $("span[data-name='OrganizationName']").text(result.Data.Organization.Name);
                    $("span[data-name='OrgBankName']").text(result.Data.Organization.BankName);
                    $("span[data-name='OrgBankBranch']").text(result.Data.Organization.BankBranch);
                    $("span[data-name='OrgBankAccount']").text(result.Data.Organization.BankAccount);
                }

                //借款订单是否更换产品
                if (result.Data.Order.ChangeProductName != "" && result.Data.Order.ChangeProductName != null) {

                    var str = result.Data.Order.ChangeProductName.split(',');
                    if (str.length == 2) {

                        //如果是分期贷
                        if (str[1] == 31021) {
                            $("#hidContractType").val(30);
                        }
                        else if (str[1] > 31021) {
                            $("#hidContractType").val(31);
                        } else {
                            $("#hidContractType").val(25);
                        }
                    }
                } else {
                    //是否机构签约
                    if (result.Data.Order.IsAgencyContract == 1) {
                        //如果是分期贷
                        if (result.Data.Order.ProductTypeId == 31021) {
                            $("#hidContractType").val(30);
                        }
                        else {
                            $("#hidContractType").val(31);
                        }
                    } else {
                        $("#hidContractType").val(25);
                    }
                }

                //显示合同Tab层
                $(".contract_generate").css("display", "block");
                loadContractInfo.GetDictionaryLoanTypeInfo();


            } else {
                alert(result.Message);
            }
        });
    $(".back").click(function () {
        window.history.back(-1);
        return false;
    });
    $(".fileList").click(function () {
        $.getJSON("../Handler/FileHandler.ashx", { OrderId: $orderId, type: 7, _: Math.random() }, function (result) {
            if (result == null) {
                $(".contract_list").text("暂无相关文件！");
            }
            else if (result.length > 0) {
                $("#contractlist").empty();
                $.each(result, function (index, item) {
                    $("#contractFileTemplate").tmpl(item).appendTo($("#contractlist"));
                });
                loadZoomScript();
            }
        });
    });
    //报表链接
    //$(".btnLoanAgreement").attr("href", "Rep_Loan_LoanAgreement.aspx?OrderId=" + $orderId);
    //$(".btnCreditAgreement").attr("href", "Rep_Loan_CreditAgreement.aspx?OrderId=" + $orderId);
    //$(".btnDebitAuth").attr("href", "Rep_Loan_DebitAuth.aspx?OrderId=" + $orderId);
    //$(".btnRepayReminder").attr("href", "Rep_Loan_RepayReminder.aspx?OrderId=" + $orderId);


});





//加载认证文件           
function loadVerifyFiles(productId) {
    $.getJSON("../Handler/ProductHandler.ashx",
        { Id: productId, Type: 5, _: Math.random() },
        function (result) {
            if (result.Status) {
                $("#necessaryFiles").empty();
                $("#optionalFiles").empty();
                $.each(result.Data, function (index, item) {
                    item.Src = "../img/4.jpg";
                    var dom = item.IsRequired ? $("#necessaryFiles") : $("#optionalFiles");
                    $("#AuthentionTemplate").tmpl(item).appendTo(dom).find(".openLayout").click(function () {
                        var authId = $(this).attr("data-authid");
                        if (filesHtml["content" + authId]) {
                            $("#authId").val(authId);
                            $("#filelist").empty();
                            $("#filelist").html(filesHtml["content" + authId]);
                            loadZoomScript();
                        }
                        else {
                            alert("还没有上传相关资料！");
                            return false;
                        }
                    });
                    bindImgData();
                });
            } else {
                alert(result.Message);
            }
        });
}
//加载脚本
function loadZoomScript() {
    $("#zoomScript").remove();
    $("#zoom").remove();
    var myScript = document.createElement("script");
    myScript.type = "text/javascript";
    myScript.id = "zoomScript";
    myScript.src = "../Plugins/gurdeZoom/js/zoom.min.js";
    document.body.appendChild(myScript);
}
//绑定图像数据
function bindImgData() {
    $.each($("#necessaryFiles,#optionalFiles").find("img"), function () {
        var authId = $(this).attr("data-authid");
        if (filesHtml["content" + authId]) {
            var matches = filesHtml["content" + authId].match(/src=\"(.*?)\"/);
            if (matches) {
                $(this).attr("src", matches[1]);
            }
        }
    });
}

//格式化日期
function ChangeDateFormat(cellval) {
    var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return date.getFullYear() + "-" + month + "-" + currentDate;
}

//更新进度条
function updateProgress(data) {
    var total = 0, count = 5;
    if (data.Order) {
        total += 100 / count;
    }
    if (data.Family) {
        total += 100 / count;
    }
    if (data.User) {
        total += 100 / count;
    }
    if (data.Work) {
        total += 100 / count;
    }
    if (data.LoanItems && data.LoanItems.length > 0) {
        total += 100 / count;
    }
    total = parseInt(total);
    $(".progressBar").text(total + "%").css("width", total + "%");
}